package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.PracticeSiteDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 实践站点
 * */

@RequestMapping("PracticeSite")
@RestController
public class PracticeSiteController extends BaseController{

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;

    @Autowired
    @Qualifier("PracticeSiteServiceImplOne")
    private PracticeSiteService practiceSiteService;

    @Autowired
    @Qualifier("CommandCenterStaffRelServiceImplOne")
    private CommandCenterStaffRelService commandCenterStaffRelService;


    //添加
    @RequestMapping(value = "AddXCX",method = RequestMethod.POST)
    public MyResult AddXCX(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String loginName = jsonObject.getString("loginName");
            String name = jsonObject.getString("name");
            String phone = jsonObject.getString("phone");
            String address = jsonObject.getString("address");
            String password = jsonObject.getString("password");


            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/PracticeSite/AddXCX");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user = userService.getByLoginName(loginName);
            if(user != null){
                return failed("手机号已注册");
            }

            user = new User();
            user.setUserCode(UUID.randomUUID().toString());
            user.setPhone(phone);
            user.setLoginName(loginName);
            user.setPassword(getMD5Str(password));
            user.setName(name);
            user.setStatus(1);
            user.setUserType(22);
            user.setCreateTime(new Date());
            userService.insert(user);

            PracticeSite practiceSite = new PracticeSite();
            practiceSite.setPracticeSiteCode(UUID.randomUUID().toString());
            practiceSite.setUserCode(user.getUserCode());
            practiceSite.setAddress(address);
            practiceSite.setCreateTime(new Date());
            practiceSite.setApplyStatus(1);
            practiceSiteService.insert(practiceSite);

            practiceSite.setApplyTime(new Date());
            practiceSiteService.update(practiceSite);

            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);

        }catch (Exception e){
            insertError("/PracticeSite/AddXCX", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //添加
    @RequestMapping(value = "AddSaaS",method = RequestMethod.POST)
    public MyResult AddSaaS(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String loginName = jsonObject.getString("loginName").trim();
            String name = jsonObject.getString("name").trim();
            String phone = jsonObject.getString("phone").trim();
            String address = jsonObject.getString("address").trim();
            String password = jsonObject.getString("password");

            String staffCode = jsonObject.getString("staffCode");


            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/PracticeSite/AddSaaS");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user = userService.getByLoginName(loginName);
            if(user != null){
                return failed("登录名已注册");
            }

            CommandCenterStaffRel commandCenterStaffRel = commandCenterStaffRelService.getByStaffCode(staffCode);

            Staff staff = staffService.getByStaffCode(staffCode);

            user = new User();
            user.setUserCode(UUID.randomUUID().toString());
            user.setPhone(phone);
            user.setLoginName(loginName);
            user.setPassword(getMD5Str(password));
            user.setName(name);
            user.setStatus(1);
            user.setUserType(22);
            user.setCreateTime(new Date());
            user.setGovernmentCode(staff.getGovernmentCode());
            userService.insert(user);

            PracticeSite practiceSite = new PracticeSite();
            practiceSite.setPracticeSiteCode(UUID.randomUUID().toString());
            practiceSite.setUserCode(user.getUserCode());
            practiceSite.setAddress(address);
            practiceSite.setCreateTime(new Date());
            practiceSite.setApplyStatus(2);
            practiceSite.setCommandCenterCode(commandCenterStaffRel.getCommandCenterCode());
            practiceSite.setGovernmentCode(staff.getGovernmentCode());
            practiceSiteService.insert(practiceSite);


            practiceSite.setApplyTime(new Date());
            practiceSite.setPassTime(new Date());
            practiceSiteService.update(practiceSite);


            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);

        }catch (Exception e){
            insertError("/PracticeSite/AddSaaS", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //编辑
    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String practiceSiteCode = jsonObject.getString("practiceSiteCode");

            String loginName = jsonObject.getString("loginName").trim();
            String name = jsonObject.getString("name").trim();
            String phone = jsonObject.getString("phone").trim();
            String address = jsonObject.getString("address").trim();
            int status = jsonObject.getIntValue("status");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/PracticeSite/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            PracticeSite practiceSite = practiceSiteService.getByPracticeSiteCode(practiceSiteCode);

            User userTemp = userService.getByLoginName(loginName);
            if(userTemp != null){
                if(!userTemp.getUserCode().equals(practiceSite.getUserCode())){
                    return failed("登录账号已被使用");
                }
            }

            practiceSite.setAddress(address);
            practiceSiteService.update(practiceSite);

            User user = userService.getByUserCode(practiceSite.getUserCode());
            user.setName(name);
            user.setLoginName(loginName);
            user.setPhone(phone);
            user.setStatus(status);
            userService.update(user);

            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/PracticeSite/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //编辑密码
    @RequestMapping(value = "EditPwd",method = RequestMethod.POST)
    public MyResult EditPwd(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String practiceSiteCode = jsonObject.getString("practiceSiteCode");

            String pwd = jsonObject.getString("password");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/VolunteerTeam/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            PracticeSite practiceSite = practiceSiteService.getByPracticeSiteCode(practiceSiteCode);
            User user = userService.getByUserCode(practiceSite.getUserCode());
            user.setPassword(getMD5Str(pwd));
            userService.update(user);

            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/VolunteerTeam/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //通过
    @RequestMapping(value = "Pass",method = RequestMethod.POST)
    public MyResult Pass(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String practiceSiteCode = jsonObject.getString("practiceSiteCode");

            int applyStatus = jsonObject.getIntValue("applyStatus");


            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/PracticeSite/Pass");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            PracticeSite practiceSite = practiceSiteService.getByPracticeSiteCode(practiceSiteCode);
            practiceSite.setApplyStatus(applyStatus);
            practiceSite.setPassTime(new Date());
            practiceSiteService.update(practiceSite);

            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/PracticeSite/Pass", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //驳回
    @RequestMapping(value = "Reject",method = RequestMethod.POST)
    public MyResult Reject(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String practiceSiteCode = jsonObject.getString("practiceSiteCode");

            int applyStatus = jsonObject.getIntValue("applyStatus");
            String msg = jsonObject.getString("msg");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/PracticeSite/Reject");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            PracticeSite practiceSite = practiceSiteService.getByPracticeSiteCode(practiceSiteCode);
            practiceSite.setApplyStatus(applyStatus);
            practiceSite.setRejectMessage(msg);
            practiceSiteService.update(practiceSite);

            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/PracticeSite/Reject", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }



    //翻页列表
    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int status = jsonObject.getIntValue("status");

            int pageIndex = jsonObject.getIntValue("pageIndex");
            int pageSize = jsonObject.getIntValue("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/PracticeSite/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            CommandCenterStaffRel commandCenterStaffRel = commandCenterStaffRelService.getByStaffCode(staffCode);

            PageInfo<PracticeSite> pageInfo = practiceSiteService.pageByCommandCenterCode(pageIndex, pageSize,
                    commandCenterStaffRel.getCommandCenterCode(),status);

            List<PracticeSiteDTO> listDTO = new ArrayList<>();
            for(PracticeSite practiceSite:pageInfo.getList()){
                PracticeSiteDTO practiceSiteDTO = mapper.map(practiceSite,PracticeSiteDTO.class);

                User user = userService.getByUserCode(practiceSite.getUserCode());
                practiceSiteDTO.setName(user.getName());
                practiceSiteDTO.setLoginName(user.getLoginName());
                practiceSiteDTO.setPhone(user.getPhone());
                practiceSiteDTO.setStatus(user.getStatus());
                listDTO.add(practiceSiteDTO);
            }

            PageInfo<PracticeSiteDTO> pageInfo1 = new PageInfo<>();
            pageInfo1.setList(listDTO);
            pageInfo1.setTotal(pageInfo.getTotal());


            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo1);

            return success(jsonObject1);
        }catch (Exception e){
            insertError("/PracticeSite/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    @RequestMapping(value = "GovPage",method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int status = jsonObject.getIntValue("status");

            int pageIndex = jsonObject.getIntValue("pageIndex");
            int pageSize = jsonObject.getIntValue("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/PracticeSite/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);


            PageInfo<PracticeSite> pageInfo = practiceSiteService.pageByGovCode(pageIndex, pageSize,
                    staff.getGovernmentCode());

            List<PracticeSiteDTO> listDTO = new ArrayList<>();
            for(PracticeSite practiceSite:pageInfo.getList()){
                PracticeSiteDTO practiceSiteDTO = mapper.map(practiceSite,PracticeSiteDTO.class);

                User user = userService.getByUserCode(practiceSite.getUserCode());
                practiceSiteDTO.setName(user.getName());
                practiceSiteDTO.setLoginName(user.getLoginName());
                practiceSiteDTO.setPhone(user.getPhone());
                practiceSiteDTO.setStatus(user.getStatus());
                listDTO.add(practiceSiteDTO);
            }

            PageInfo<PracticeSiteDTO> pageInfo1 = new PageInfo<>();
            pageInfo1.setList(listDTO);
            pageInfo1.setTotal(pageInfo.getTotal());


            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo1);

            return success(jsonObject1);
        }catch (Exception e){
            insertError("/PracticeSite/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //小程序翻页列表
    @RequestMapping(value = "PageXCX",method = RequestMethod.POST)
    public MyResult PageXCX(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String commandCenterCode = jsonObject.getString("commandCenterCode");

            int pageIndex = jsonObject.getIntValue("pageIndex");
            int pageSize = jsonObject.getIntValue("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/PracticeSite/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            PageInfo<PracticeSite> pageInfo = practiceSiteService.pageByCommandCenterCode(pageIndex, pageSize,
                    commandCenterCode,2);

            List<PracticeSiteDTO> listDTO = new ArrayList<>();
            for(PracticeSite practiceSite:pageInfo.getList()){
                PracticeSiteDTO practiceSiteDTO = mapper.map(practiceSite,PracticeSiteDTO.class);

                User user = userService.getByUserCode(practiceSite.getUserCode());
                practiceSiteDTO.setName(user.getName());
                practiceSiteDTO.setLoginName(user.getLoginName());
                practiceSiteDTO.setPhone(user.getPhone());
                practiceSiteDTO.setStatus(user.getStatus());
                listDTO.add(practiceSiteDTO);
            }

            PageInfo<PracticeSiteDTO> pageInfo1 = new PageInfo<>();
            pageInfo1.setList(listDTO);
            pageInfo1.setTotal(pageInfo.getTotal());


            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo1);

            return success(jsonObject1);
        }catch (Exception e){
            insertError("/PracticeSite/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //待审核翻页列表
    @RequestMapping(value = "PageApply",method = RequestMethod.POST)
    public MyResult PageApply(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int status = jsonObject.getIntValue("status");
            int pageIndex = jsonObject.getIntValue("pageIndex");
            int pageSize = jsonObject.getIntValue("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/PracticeSite/PageApply");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            CommandCenterStaffRel commandCenterStaffRel = commandCenterStaffRelService.getByStaffCode(staffCode);

            PageInfo<PracticeSite> pageInfo = practiceSiteService.pageByCommandCenterCodeApply(pageIndex, pageSize,
                    commandCenterStaffRel.getCommandCenterCode());

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo);

            return success(jsonObject1);
        }catch (Exception e){
            insertError("/PracticeSite/PageApply", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }




    //登录
    @RequestMapping(value = "Login",method = RequestMethod.POST)
    public MyResult Login(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String loginName = jsonObject.getString("loginName").trim();
            String password = jsonObject.getString("password");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/PracticeSite/Login");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user = userService.getByLoginNameAndPassword(loginName,getMD5Str(password));
            if(user == null){
                return failed("用户名或者密码错误");
            }
            if(user.getStatus() == 2){
                return failed("账户被禁用");
            }
            if(user.getUserType() != 22){
                return failed("账户类型错误");
            }


            PracticeSite practiceSite = practiceSiteService.getByUserCode(user.getUserCode());


            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("user",user);
            jsonObject1.put("practiceSite",practiceSite);

            return success(jsonObject1);
        }catch (Exception e){
            insertError("/PracticeSite/Login",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


}
